package com.qf.dao;

import com.qf.pojo.Student;
import com.qf.util.DataSourceUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 千锋健哥
 */
public class StudentDaoImpl implements StudentDao {

    //1. 创建全局的QueryRunner对象
    private QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());

    @Override
    public void insert(Student student) {
        String sql = "insert into student(name, age, birthday, sex, clazz) VALUES(?, ?, ?, ?, ?)";
        try {
            Long count = queryRunner.insert(sql, new ScalarHandler<Long>(), student.getName(),
                    student.getAge(), student.getBirthday(), student.getSex(), student.getClazz());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void update(Student student) {

    }

    @Override
    public void delete(Integer id) {

    }

    @Override
    public List<Student> findAll() {
        String sql = "select * from student";
        try {
            List<Student> list = queryRunner.query(sql, new BeanListHandler<Student>(Student.class));
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

}
